![]() |
PATH![]() |
![]() ![]() |
An operation is an expression that uses an operator to derive a value from other values, called operands . AppleScript includes operators for performing arithmetic operations, comparing values, performing Boolean evaluations, and coercing values.
Each operator can handle operands of specific classes, which are defined in the definition of the operator. For example, the operands for the addition ( + ) operator must belong to the class Integer or Real, while the operand for the Not operator must belong to class Boolean. Certain operators work with operands from a variety of classes. For example, you can use the concatenation operator ( & ) to join two strings, two lists, or two records.
The result of each operation is a value of a particular class. For many operators, such as the equality operator ( = ) and the greater than operator ( > ), the class of the result is always the same--in these cases, Boolean. For other operators, such as the concatenation operator (&), the class of the result depends on the class of the operands. For example, the result of concatenating two strings is a string, but the result of concatenating two integers is a list of integers.
If you use an operator with operands of the wrong classes, AppleScript attempts to coerce the operands to the correct class, if possible. For example, the concatenation operator (&) works with strings, lists, or records. When AppleScript evaluates the following expression, it coerces the integer 66 to a string before concatenating it with the string "Route" .
"Route " & 66
--result: "Route 66"
For more information, see Concatenation.
When evaluating expressions containing operators, AppleScript checks the leftmost operand first. If the operand does not belong to one of the legal classes for the operator, AppleScript coerces it if possible. After coercing the leftmost operand or verifying that it belongs to a legal class, AppleScript checks the rightmost operand and coerces it (if necessary and possible) to be compatible with the leftmost operand. The exceptions to this rule are expressions with the Is Contained By, Equal, and Is Not Equal operators. AppleScript checks the rightmost operand first in expressions with the Is Contained By operator. AppleScript never coerces operands of the Equal and Is Not Equal operators.
If AppleScript cannot coerce the operands, it returns an error. For example, the addition operator (+) works with numbers (integers and real numbers) only. If you attempt to evaluate an expression such as 3 + "cat" , you'll get an error, because AppleScript cannot coerce "cat" to a number.
Operations can be performed either by AppleScript or by an application. The rule is that if the leftmost operand is a value, AppleScript performs the operation, and if the leftmost operand is a reference to an application object, the application performs the operation. For example, the comparison
tell application "AppleWorks"
"Hello" contains word 1 of text body of document "Simple"
end tell
is performed by AppleScript, because the first operand is a string. Before performing the comparison, AppleScript must get the value of the first word. In contrast, the application specified in the Tell statement, AppleWorks, performs the following comparison
tell application "AppleWorks"
word 1 of text body of document "Simple" contains "Hello"
end tell
The Is Contained By operator is an exception to this rule. In expressions with the Is Contained By operator, AppleScript performs the operation if the rightmost operand is a value and the application performs the operation if the rightmost operand is a reference to an application object. For more information, see Contains, Is Contained By.
Table 6-1 summarizes the AppleScript operators. For each operator, it includes a brief description of the operation and lists the class (or classes) of the operands and the class (or classes) of the result. A few of the operators are characters that you type with modifier keys. For these operators, the keystrokes are shown in parentheses. Operators That Handle Operands of Various Classes provides more information about how operators treat different classes of operands, along with more detailed explanations and examples of operations.
Table 6-1 AppleScript operators
and
|
And. Binary logical operator that results in
true
if both the operand to its left and the operand to its right are
true
. Both of the operands must evaluate to Boolean values. When evaluating expressions containing the And operator, AppleScript checks the leftmost operand first. If its value is
false
, AppleScript does not evaluate the rightmost operand, because it already knows the expression is
false
. (This behavior is sometimes called
short-circuiting.) Class of operands. Boolean Class of result. Boolean |
or
|
Or. Binary logical operator that results in
true
if either the operand to its left or the operand to its right is
true
. The leftmost operand must evaluate to a Boolean value because it is always checked first when AppleScript evaluates an expression containing the Or operator. If its value is
true
, AppleScript does not evaluate the rightmost operand, because it already knows the expression is
true
. (This behavior is sometimes called short-circuiting.) Class of operands. Boolean Class of result. Boolean |
&
|
Concatenation. Binary operator that joins two values. If the operand to the left of the operator is a string, the result is a string. If the operand to the left of the operator is a record, the result is a record. If the operand to the left of the operator belongs to any other class, the result is a list. For more information, see
Concatenation. Class of operands. Any Class of result. List, Record, String |
=
|
Equal. Binary comparison operator that results in
true
if the operand to its left and the operand to its right have the same value. The operands can be of any class. The method AppleScript uses to determine equality depends on the class of the operands--for more information, see
Equal, Is Not Equal To. Class of operands. Any Class of result. Boolean |
≠
(Option-equal sign)
|
Not equal. Binary comparison operator that results in
true
if the operand to its left and the operand to its right have different values. The operands can be of any class. The method AppleScript uses to determine equality depends on the class of the operands--for more information, see
Equal, Is Not Equal To. Class of operands. Any Class of result. Boolean |
>
|
Greater than. Binary comparison operator that results in
true
if the value of the operand to its left is greater than the value of the operand to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. The method AppleScript uses to determine which value is greater depends on the class of the operands--for more information, see
Greater Than, Less Than. Class of operands. Date, Integer, Real, String Class of result. Boolean |
<
|
Less than. Binary comparison operator that results in
true
if the value of the operand to its left is less than the value of the operand to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. The method AppleScript uses to determine which value is greater depends on the class of the operands--for more information, see
Greater Than, Less Than. Class of operands. Date, Integer, Real, String Class of result. Boolean |
≥
(Option-greater-than sign)
|
Greater than or equal to. Binary comparison operator that results in
true
if the value of the operand to its left is greater than or equal to the value of the operand to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. The method AppleScript uses to determine which value is greater depends on the class of the operands. Class of operands. Date, Integer, Real, String Class of result. Boolean |
≤
(Option-less-than sign)
|
Less than or equal to. Binary comparison operator that results in
true
if the value of the operand to its left is less than or equal to the value of the operand to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. The method AppleScript uses to determine which value is greater depends on the class of the operands. Class of operands. Date, Integer, Real, String Class of result. Boolean |
start[s] with
|
Starts with. Binary containment operator that results in
true
if the list or string to its right matches the beginning of the list or string to its left. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. For more information, see
Starts With, Ends With. Class of operands. List, String Class of result. Boolean |
end[s] with
|
Ends with. Binary containment operator that results in
true
if the list or string to its right matches the end of the list or string to its left. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. For more information, see
Starts With, Ends With. Class of operands. List, String Class of result. Boolean |
contain[s]
|
Contains. Binary containment operator that results in
true
if the list, record, or string to its right matches any part of the list, record, or string to its left. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. For more information, see
Contains, Is Contained By. Class of operands. List, Record, String Class of result. Boolean |
does not contain
|
Does not contain. Binary containment operator that results in
true
if the list, record, or string to its right does not match any part of the list, record, or string to its left. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the right of the operator to the class of the operand to the left. Class of operands. List, Record, String Class of result. Boolean |
is in
|
Is contained by. Binary containment operator that results in
true
if the list, record, or string to its left matches any part of the list, record, or string to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the left of the operator to the class of the operand to the right. For more information, see
Contains, Is Contained By. Class of operands. List, Record, String Class of result. Boolean |
is not in
|
Is not contained by. Binary containment operator that results in
true
if the list, record, or string to its left does not match any part of the list, record, or string to its right. Both operands must evaluate to values of the same class. If they don't, AppleScript attempts to coerce the operand to the left of the operator to the class of the operand to the right. Class of operands. List, Record, String Class of result. Boolean |
*
|
Multiply. Binary arithmetic operator that multiplies the number to its left and the number to its right. Class of operands. Integer, Real Class of result. Integer, Real |
+
|
Plus. Binary arithmetic operator that adds the number or date to its left and the number or date to its right. Only integers can be added to dates. AppleScript interprets such an integer as a number of seconds. Class of operands. Date, Integer, Real Class of result. Date, Integer, Real |
-
|
Minus. Binary or unary arithmetic operator. The binary operator subtracts the number to its right from the number or date to its left. The unary operator makes the number to its right negative. Only integers can be subtracted from dates. AppleScript interprets such an integer as a number of seconds. Class of operands. Date, Integer, Real Class of result. Date, Integer, Real |
÷
(Option-slash)
|
Division. Binary arithmetic operator that divides the number to its left by the number to its right. Class of operands. Integer, Real Class of result. Real |
div
|
Integral division. Binary arithmetic operator that divides the number to its left by the number to its right and returns the integral part of the answer as its result. Class of operands. Integer, Real Class of result. Integer |
mod
|
Remainder. Binary arithmetic operator that divides the number to its left by the number to its right and returns the remainder as its result. Class of operands. Integer, Real Class of result. Integer, Real |
^
|
Exponent. Binary arithmetic operator that raises the number to its left to the power of the number to its right. Class of operands. Integer, Real Class of result. Real |
as
|
Coercion. Binary operator that converts the operand to its left to the class listed to its right. Not all values can be coerced to all classes. The coercions that AppleScript can perform are listed in
Coercing Values. The additional coercions, if any, that applications can perform are listed in application dictionaries. Class of operands. the operand to the right of the operator must be a class identifier; the operand to the left must be a value that can be converted to that class Class of result. the class specified by the class identifier to the right of the operator |
not
|
Not. Unary logical operator that results in
true
if the operand to its right is
false
, and
false
if the operand to its right is
true
. Class of operand. Boolean Class of result. Boolean |
[a] ( ref [to] | reference to )
|
A Reference To. Unary operator that causes AppleScript to interpret the value to its right as a reference instead of getting its value. For more information about the A Reference To operator, see
The A Reference To Operator. Class of operand. Reference Class of result. Reference |